//
//  AppDelegate.swift
//  SwiftProject
//
//  Created by Lausen on 2017/7/4.
//  Copyright © 2017年 id. All rights reserved.
//

import UIKit
import UserNotifications
import CoreData
import SVProgressHUD
import AFNetworking

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        loadAdditions()
        
        window = UIWindow()
        window?.backgroundColor = UIColor.white
        window?.rootViewController = LauMainViewController()
        window?.makeKeyAndVisible()
        
        loadAppInfo()
        
        return true
    }
}

extension AppDelegate{
    
    fileprivate func loadAdditions(){
        
        //1:设置提示框的显示时长
        SVProgressHUD.setMaximumDismissTimeInterval(2)
        
        //2:设置AFN的网络加载指示器
        AFNetworkActivityIndicatorManager.shared().isEnabled = true
        
        //3:获取用户的允许权限
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.badge,.carPlay,.sound]) { (success, error) in
                
                print("授权" + (success ? "成功" : "失败"))
            }
        } else {
            // Fallback on earlier versions
            /// 请求用户授权
            let notifySettings = UIUserNotificationSettings(types: [.alert,.badge,.sound], categories: nil)
            UIApplication.shared.registerUserNotificationSettings(notifySettings)
        }
        
    }
}

// MARK: - 加载应用程序的信息,其实就是动态的设置应用程序的界面
extension AppDelegate{

    fileprivate func loadAppInfo(){
        
        DispatchQueue.global().async { 
            
            guard let url = Bundle.main.url(forResource: "main.json", withExtension: nil) else{
                
                return
            }
            
            let data = NSData(contentsOf: url)
            
            let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            
            let jsonPath = (path as NSString).appendingPathComponent("main.json")
            
            data?.write(toFile: jsonPath, atomically: true)
            
            print("当前数据已经存入沙盒\(path)")
        }
    }
}

